using System;

namespace LicenseApp.Client.Examples;

/// <summary>
/// 授权信息获取演示
/// </summary>
public static class LicenseInfoDemo
{
    /// <summary>
    /// 演示所有授权信息获取方法
    /// </summary>
    public static void RunDemo()
    {
        Console.WriteLine("=== 授权信息获取演示 ===\n");

        // 1. 获取完整摘要
        Console.WriteLine("1. 完整授权信息摘要:");
        Console.WriteLine(new string('-', 40));
        string summary = LicenseInfoHelper.GetLicenseSummary();
        Console.WriteLine(summary);
        Console.WriteLine();

        // 2. 获取简洁信息
        Console.WriteLine("2. 简洁授权信息:");
        Console.WriteLine(new string('-', 40));
        string brief = LicenseInfoHelper.GetLicenseBrief();
        Console.WriteLine(brief);
        Console.WriteLine();

        // 3. 获取设备授权信息
        Console.WriteLine("3. 设备授权信息:");
        Console.WriteLine(new string('-', 40));
        var deviceAuth = LicenseInfoHelper.GetDeviceAuthInfo();
        Console.WriteLine($"是否授权: {deviceAuth.IsAuthorized}");
        Console.WriteLine($"最大AGV数: {deviceAuth.MaxAgvCount} 台");
        Console.WriteLine($"状态: {deviceAuth.StatusMessage}");
        Console.WriteLine($"剩余天数: {deviceAuth.RemainingDays} 天");
        Console.WriteLine($"到期日期: {deviceAuth.ExpiryDate:yyyy-MM-dd}");
        Console.WriteLine();

        // 4. 获取详细信息对象
        Console.WriteLine("4. 详细授权信息:");
        Console.WriteLine(new string('-', 40));
        var detailedInfo = LicenseInfoHelper.GetDetailedLicenseInfo();
        Console.WriteLine($"有授权: {detailedInfo.HasLicense}");
        Console.WriteLine($"AGV数量: {detailedInfo.AgvCount} 台");
        Console.WriteLine($"状态: {detailedInfo.StatusText}");
        Console.WriteLine($"是否有效: {detailedInfo.IsActive}");
        Console.WriteLine($"生效日期: {detailedInfo.StartDate:yyyy-MM-dd}");
        Console.WriteLine($"到期日期: {detailedInfo.EndDate:yyyy-MM-dd}");
        Console.WriteLine($"总天数: {detailedInfo.TotalDays} 天");
        Console.WriteLine($"剩余天数: {detailedInfo.RemainingDays} 天");
        Console.WriteLine($"即将到期: {detailedInfo.IsExpiringSoon}");
        Console.WriteLine($"在宽限期: {detailedInfo.IsInGracePeriod}");
        Console.WriteLine();

        // 5. 表格格式
        Console.WriteLine("5. 表格格式:");
        Console.WriteLine(new string('-', 40));
        string table = LicenseInfoHelper.GetLicenseTable();
        Console.WriteLine(table);
        Console.WriteLine();

        // 6. JSON格式
        Console.WriteLine("6. JSON格式:");
        Console.WriteLine(new string('-', 40));
        string json = LicenseInfoHelper.GetLicenseJson();
        Console.WriteLine(json);
        Console.WriteLine();

        // 7. 实际应用示例
        Console.WriteLine("7. 实际应用示例:");
        Console.WriteLine(new string('-', 40));
        DemoBusinessLogic();
    }

    /// <summary>
    /// 演示业务逻辑中的使用
    /// </summary>
    private static void DemoBusinessLogic()
    {
        // 检查AGV授权
        var deviceAuth = LicenseInfoHelper.GetDeviceAuthInfo();
        
        Console.WriteLine("AGV控制系统启动检查:");
        
        if (!deviceAuth.IsAuthorized)
        {
            Console.WriteLine("[错误] 系统未授权，无法启动AGV控制功能");
            return;
        }

        Console.WriteLine($"[成功] 系统已授权，可控制最多 {deviceAuth.MaxAgvCount} 台AGV");

        // 模拟当前连接的AGV数量
        int currentAgvCount = 3;

        Console.WriteLine($"当前连接AGV: {currentAgvCount} 台");

        if (currentAgvCount < deviceAuth.MaxAgvCount)
        {
            int availableSlots = deviceAuth.MaxAgvCount - currentAgvCount;
            Console.WriteLine($"[成功] 还可以连接 {availableSlots} 台AGV");
        }
        else
        {
            Console.WriteLine("[警告] 已达到授权上限，无法连接更多AGV");
        }

        // 检查到期提醒
        if (deviceAuth.RemainingDays <= 7 && deviceAuth.RemainingDays > 0)
        {
            Console.WriteLine($"[警告] 注意: 授权将在 {deviceAuth.RemainingDays} 天后到期，请及时续期");
        }
        else if (deviceAuth.RemainingDays <= 0)
        {
            Console.WriteLine("[错误] 授权已过期，系统功能可能受限");
        }
        
        // 显示状态栏信息
        string statusBarText = LicenseInfoHelper.GetLicenseBrief();
        Console.WriteLine($"状态栏显示: {statusBarText}");
    }

    /// <summary>
    /// 快速获取关键信息的示例
    /// </summary>
    public static void QuickInfoDemo()
    {
        Console.WriteLine("=== 快速信息获取 ===\n");

        // 一行代码获取关键信息
        int agvCount = LicenseInfoHelper.GetDeviceAuthInfo().MaxAgvCount;
        bool isAuthorized = LicenseInfoHelper.GetDeviceAuthInfo().IsAuthorized;
        int remainingDays = LicenseInfoHelper.GetDeviceAuthInfo().RemainingDays;
        string statusText = LicenseInfoHelper.GetDeviceAuthInfo().StatusMessage;

        Console.WriteLine($"AGV数量: {agvCount}");
        Console.WriteLine($"是否授权: {isAuthorized}");
        Console.WriteLine($"剩余天数: {remainingDays}");
        Console.WriteLine($"状态: {statusText}");
        
        // 状态栏文本
        string brief = LicenseInfoHelper.GetLicenseBrief();
        Console.WriteLine($"状态栏: {brief}");
    }
}
